<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增银行账户' : '编辑银行账户'"
    :visible.sync="visible"
    width="500px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right" label-width="100px">
      <el-form-item label="银行卡号:" prop="cardNo" required>
        <el-input v-model.trim="temp.cardNo" :maxlength="30" :disabled="type==='edit'" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="开户银行:" prop="bankName">
        <el-input v-model.trim="temp.bankName" :maxlength="150" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="账户名:" prop="accountName">
        <el-input v-model.trim="temp.accountName" :maxlength="20" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="使用机构" prop="orgId">
        <el-tree-select
          ref="treeSelect"
          v-model="temp.orgId"
          clearable
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择',
          }"
          :tree-params=" {
            'expand-on-click-node': false,
            'highlight-current': true,
            'filterable': true,
            filterable:true,
            props:{
              value:'orgId',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
          @search-clear="searchClear"
        />
      </el-form-item>
      <el-form-item label="是否启用:" prop="accountStatus">
        <el-select v-model="temp.accountStatus" filterable style="width: 100%;">
          <el-option :value="1" label="启用" />
          <el-option :value="2" label="禁用" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryBankCardNoHas, saveBankAccount } from '@/api'

  export default {
    name: 'add',
    props: {
      treeData: {
        type: Array,
        required: true
      },
      orgList: {
        type: Array,
        required: true
      }
    },
    data() {
      const validCardNo = (rule, value, callback) => {
        if (value !== '') {
          const reg = /[a-zA-Z0-9]{8,30}/
          if (!reg.test(value)) {
            callback(new Error('请输入正确的银行卡号'))
          }
          callback()
        } else {
          callback(new Error('请输入银行卡号'))
        }
      }
      return {
        visible: false,
        type: 'add',
        preOrgId: '',
        temp: {
          bankName: '',
          cardNo: '',
          accountName: '',
          orgId: '',
          accountStatus: 1
        },
        rules: {
          bankName: [{ required: true, message: '请输入开户银行', trigger: 'change' }],
          cardNo: [{ validator: validCardNo, trigger: 'blur' }],
          accountName: [{ required: true, message: '请输入账户名', trigger: 'change' }],
          orgId: [{ required: true, message: '请选择使用机构', trigger: 'change' }]
        }
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
            this.preOrgId = temp.orgId
            this.type = 'edit'
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(async valid => {
          if (!valid) return
          const params = {
            bankName: this.temp.bankName,
            cardNo: this.temp.cardNo,
            accountName: this.temp.accountName,
            accountStatus: this.temp.accountStatus
          }
          if (this.type === 'edit') {
            const org = this.orgList.find(item => item.orgId === this.temp.orgId)
            params.newOrgId = this.temp.orgId
            params.newOrgSNNo = org.orgSNNo
            params.newOrgName = org.orgName
            params.orgId = this.preOrgId
          } else {
            const org = this.orgList.find(item => item.orgId === this.temp.orgId)
            params.orgId = this.temp.preOrgId
            params.orgSNNo = org.orgSNNo
            params.orgName = org.orgName
          }
          try {
            await this.searchCardNo(params)
            if (this.type === 'add') {
              this.add(params)
            } else {
              this.edit(params)
            }
          } catch (e) {
            this.$message.error('银行卡号已存在,请查看是否启用')
          }
        })
      },
      async searchCardNo(params) {
        await queryBankCardNoHas({
          cardNo: params.cardNo,
          orgId: params.orgId
        })
      },
      async add(params) {
        await saveBankAccount(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
      },
      async edit(params) {
        await saveBankAccount(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.type = 'add'
        this.temp = {
          bankName: '',
          cardNo: '',
          accountName: '',
          orgId: '',
          accountStatus: 1
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      },
      // 清空机构搜索框
      searchClear() {
        if (this.$refs.treeSelect) this.$refs.treeSelect.keywords = ''
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 100%;
  }
}
</style>
